package com.cuitrip.business.user.modules;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cuitrip.component.choicedialog.ChoiceDialog;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.utils.Utils;

/**
 * Created by MiJack on 2015/10/16.
 */
public class MInput1Activity extends CustomUiFragmentActivity {

    public static final String KEY_PLACE_HOLDER         = "KEY_PLACE_HOLDER";
    public static final String KEY_TIP                  = "KEY_TIP";
    public static final String KEY_VALUE                = "KEY_VALUE";
    public static final String KEY_TITLE                = "KEY_TITLE";
    public static final String KEY_VALUE_MAX_LENGTH     = "KEY_VALUE_MAX_LENGTH";
    public static final String KEY_RESULT               = "KEY_RESULT";
    public static final String KEY_DIALOG_TITLE         = "KEY_DIALOG_TITLE";
    public static final String KEY_DIALOG_CONTENT       = "KEY_DIALOG_CONTENT";
    public static final String KEY_SHOW_CONFIRM_DIALOG  = "KEY_SHOW_CONFIRM_DIALOG";
    public static final String KEY_INPUT_TYPE           = "KEY_INPUT_TYPE";
    String placeHolder;
    String value;
    String tip;
    int maxLength;
    String title;
    boolean showConfirmDialog;
    String dialogTitle;
    String dialogContent;
    private EditText editMInput1;
    private TextView textMInput1;
    private int inputType;
    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        Intent intent = getIntent();
        if (intent == null||intent.getExtras()==null) {
            return null;
        }
        initData(intent.getExtras());
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName=title;
        config.mTopbarRightAreaName=getString(R.string.operation_ok_1);
        config.mRightStyle= CustomUiConfig.AreaStyle.TEXT_STYLE;
        return config;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_minput1);
    }
    @Override
    public void initView() {
        setResult(RESULT_CANCELED);
        editMInput1= (EditText) findViewById(R.id.editMInput1);
        editMInput1.setInputType(inputType);
        textMInput1= (TextView) findViewById(R.id.textMInput1);
        editMInput1.setHint(placeHolder);
        editMInput1.setText(value);
        if (TextUtils.isEmpty(tip)){
            textMInput1.setVisibility(View.GONE);
        }else{
            textMInput1.setText(tip);
        }
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }

    @Override
    public void onTopbarRightPress() {
        trySubmit();
    }

    private void initData(Bundle args) {
        placeHolder        = args.getString(KEY_PLACE_HOLDER);
        maxLength          = args.getInt(KEY_VALUE_MAX_LENGTH, Integer.MAX_VALUE);
        title              = args.getString(KEY_TITLE);
        value              = args.getString(KEY_VALUE);
        tip                = args.getString(KEY_TIP);
        showConfirmDialog  = args.getBoolean(KEY_SHOW_CONFIRM_DIALOG, false);
        inputType          = args.getInt(KEY_INPUT_TYPE, InputType.TYPE_CLASS_TEXT);
        if (showConfirmDialog) {
            dialogTitle   = args.getString(KEY_DIALOG_TITLE);
            dialogContent = args.getString(KEY_DIALOG_CONTENT);
        }
    }

    public static void startEditActivityForResult(Activity activity, Bundle args, int requestCode) {
        Intent intent = new Intent(activity, MInput1Activity.class);
        intent.putExtras(args);
        activity.startActivityForResult(intent, requestCode);
    }

    public void trySubmit() {
                final String result=editMInput1.getText().toString();
                if (result.length() > maxLength) {
                    Toast.makeText(this,getString(R.string.user_attribute_work_limit,maxLength),Toast.LENGTH_SHORT).show();
                    return;
                }
                if (result.length()==0){
                    Utils.shakeView(editMInput1);
                    return;
                }
                if (showConfirmDialog) {
                    ChoiceDialog choiceDialog=ChoiceDialog.getInstance();
                    choiceDialog.setDialogTitle(dialogTitle);
                    choiceDialog.setDialogTitleDividerVisibility(false);
                    choiceDialog.setDialogMessage(String.format(dialogContent,result));
                    choiceDialog.setDialogCoupleStyleSetting(
                            getResources().getString(R.string.operation_yes),
                            getResources().getString(R.string.operation_cancel), new ChoiceDialog.DialogStyleCoupleCallback() {
                        @Override
                        public boolean onPositiveButtonClick() {
                            finish(result);
                            return false;
                        }

                        @Override
                        public boolean onNegativeButtonClick() {
                            return false;
                        }
                    });
                    showDialogFragment(choiceDialog);
                } else {
                    finish(result);
                }
    }

    private void finish(String result) {
        setResult(RESULT_OK, new Intent().putExtra(KEY_RESULT, result));
        finish();
    }


}
